package com.authority.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.common.AuthConstants;
import com.authority.entry.AuthUserRoleDO;
import com.authority.service.AuthRoleService;
import com.authority.service.AuthUserRoleService;
import com.google.common.collect.Lists;
import com.suixun.util.Constant;

/**
 * 用户角色关系管理
 * 
 * @author wb-liyuan.j
 *
 * @modify leizhendong
 */

@Controller
@RequestMapping(value = "/auth/userRoleManager")
public class UserRoleManager extends BaseManager {
	@Resource
	private AuthUserRoleService authUserRoleService;

	@Resource
	private AuthRoleService authRoleService;

	/**
	 * 用户列表查询
	 * 
	 * @param sysId
	 *            所属系统
	 * @param userId
	 *            用户id
	 * @param account
	 *            账户
	 * @param username
	 *            用户名
	 * @param roleId
	 *            角色id
	 * @param status
	 *            状态
	 * @return
	 */
	
	@RequestMapping(value = "/listUser.json")
	@ResponseBody
	public Object doListUser(Long sysId, Long userId, String account,String userName,Long roleId,Integer status, int index) {

		try {

			checkParams(sysId);
			return success(authUserRoleService.listUser(sysId, userId, account, userName, roleId, status, index));
		} catch (Exception e) {
			logger("UserManager doListUser:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * 获取指定用户的所有角色名称
	 * 
	 * @param sysId
	 * @param userId
	 * @return
	 */
	
	@RequestMapping(value = "/getRoleIdsByUserId.json")
	@ResponseBody
	public Object doGetRoleIdsByUserId( Long sysId,Long userId) {

		try {
			checkParams(userId, sysId);

			return success(authUserRoleService.getRoleIdsByUserId(sysId, userId));
		} catch (Exception e) {
			logger("UserManager doGetRoleIdsByUserId:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * 更新用户角色关系
	 * 
	 * @param sysId
	 * @param userId
	 * @param roleIds
	 *            角色id列表
	 * @return
	 */
	@RequestMapping(value = "/updateUserRole.json")
	@ResponseBody
	public Object doUpdateUserRole(Long sysId,Long userId, String roleIds) {

		try {
			checkParams(userId, sysId);

			List<AuthUserRoleDO> list = Lists.newArrayList();

			if (StringUtils.isNotEmpty(roleIds)) {
				String[] arr = roleIds.split(AuthConstants.SEPARATOR_COMMA);
				for (String roleId : arr) {
					AuthUserRoleDO db = new AuthUserRoleDO();
					db.setUserId(userId);
					db.setRoleId(NumberUtils.toLong(roleId));
					db.setSysId(sysId);
					db.setOperatorId(getUserId());
					list.add(db);
				}
			}

			authUserRoleService.modifyAuthUserRole(userId, sysId, list);

			return success();
		} catch (Exception e) {
			logger("UserManager doUpdateUserRole:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

}
